package br.edu.sistemaacademico.builder;



import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.pojo.PrototypeFactory;
import br.edu.sistemaacademico.pojo.Servidor;

public class ServidorDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"codigo", "nome", "cpf", "endereco", "telefone", "sexo", "rg",
			"ctps", "escolaridade", "cargo", "login", "senha", "status" });

	@SuppressWarnings("unchecked")
	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Servidor s = (Servidor) o;
		List<Object> valores = Arrays.asList(new Object[] { s.getId(),
				s.getNome(), s.getCpf(), s.getEndereco(), s.getTelefone(),
				s.getSexo(), s.getRg(), s.getCtps(),
				s.getEscolaridade(), s.getCargo(), s.getLogin(), s.getSenha(), s.getStatus() });

		String condicao = "codigo = " + s.getId() + " and status =1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_SERVIDOR");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Servidor s  = (Servidor) PrototypeFactory.createPrototypeFactory("servidor");
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Servidor a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao+" and status=1");
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				s = (Servidor) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return s;
	}

	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		List<Servidor> list = new LinkedList<Servidor>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_Servidor a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		select.setCondicao(condicao+" and status =1");
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next())
				list.add((Servidor) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}
	@Override
	public void gravar(Object o) throws SQLException {
		Servidor s = (Servidor) o;
		List<Object> valores = Arrays.asList(new Object[] { s.getId(),
				s.getNome(), s.getCpf(), s.getEndereco(), s.getTelefone(),
				s.getSexo(), s.getRg(), s.getCtps(),
				s.getEscolaridade(), s.getCargo(), s.getLogin(), s.getSenha(),1 });

		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_Servidor");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);

		try {
			System.out.println(insert.getSQL());
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Servidor a  = (Servidor) PrototypeFactory.createPrototypeFactory("servidor");
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setNome(rs.getString(COLUNAS.get(1)));
		a.setCpf(rs.getString(COLUNAS.get(2)));
		a.setEndereco(rs.getString(COLUNAS.get(3)));
		a.setTelefone(rs.getString(COLUNAS.get(4)));
		a.setSexo(rs.getString(COLUNAS.get(5)).charAt(0));
		a.setRg(rs.getString(COLUNAS.get(6)));
		//a.setNascimento(rs.getString(COLUNAS.get(7)));
		a.setCtps(rs.getString(COLUNAS.get(7)));
		a.setEscolaridade(rs.getString(COLUNAS.get(8)));
		a.setCargo(rs.getString(COLUNAS.get(9)));
		a.setLogin(rs.getString(COLUNAS.get(10)));
		a.setSenha(rs.getString(COLUNAS.get(11)));
		a.setStatus(rs.getInt(COLUNAS.get(12)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Servidor s = (Servidor) o;
		List<Object> valores = Arrays.asList(new Object[] { s.getId(),
				s.getNome(), s.getCpf(), s.getEndereco(), s.getTelefone(),
				s.getSexo(), s.getRg(), s.getCtps(),
				s.getEscolaridade(), s.getCargo(), s.getLogin(), s.getSenha(), s.getStatus() });

		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Servidor");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
	public void deletar(Object o) {
		Servidor s = (Servidor) o;
		List<Object> valores = Arrays.asList(new Object[] { s.getId(),
				s.getNome(), s.getCpf(), s.getEndereco(), s.getTelefone(),
				s.getSexo(), s.getRg(), s.getCtps(),
				s.getEscolaridade(), s.getCargo(), s.getLogin(), s.getSenha(), 0 });

		String condicao = "codigo = " + s.getId() + " and status =1";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_SERVIDOR");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
